package com.xoa.service.party;

import com.xoa.dao.party.InspectionRecordMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.InspectionRecord;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/11.
 */
@Service
public class InspectionRecordService {
    @Autowired
    InspectionRecordMapper inspectionRecordMapper;

    public BaseWrapper addRecord(InspectionRecord inspectionRecord){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(inspectionRecord!=null){
            int in = inspectionRecordMapper.insert(inspectionRecord);
            if(in>0){
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("ok");
            }
        }
        return  baseWrapper;
    }

    /**
     * @Author 程叶同
     * @Description 修改记录
     * @Date 14:03 2018/1/10
     */

    public BaseWrapper updateRecord(InspectionRecord inspectionRecord){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(inspectionRecord!=null){
            int i = inspectionRecordMapper.updateByPrimaryKey(inspectionRecord);
            if(i>0){
                baseWrapper.setMsg("修改成功");
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
            }
        }
        return baseWrapper;
    }


    public BaseWrapper deleteRecord(Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=null){
            int i = inspectionRecordMapper.deleteByPrimaryKey(id);
            if(i>0){
                baseWrapper.setMsg("删除成功");
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
            }
        }
        return baseWrapper;
    }
    /**
     * @Author 程叶同
     * @Description 通过id查询
     * @Date 14:10 2018/1/10
     */

    public BaseWrapper getRecordsById(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        if(id!=null&&id!=0){
            InspectionRecord inspectionRecord = inspectionRecordMapper.selectByPrimaryKey(id);
            List<Attachment> attachmentbefore = GetAttachmentListUtil.returnAttachment(inspectionRecord.getAttachmentNameBefore(), inspectionRecord.getAttachmentIdBefore(), sqlType, GetAttachmentListUtil.MODULE_parameter);
            List<Attachment> attachmentafter = GetAttachmentListUtil.returnAttachment(inspectionRecord.getAttachmentNameAfter(), inspectionRecord.getAttachmentIdAfter(), sqlType, GetAttachmentListUtil.MODULE_parameter);
            inspectionRecord.setAttachmentListBefore(attachmentbefore);
            inspectionRecord.setAttachmentListAfter(attachmentafter);
            if(inspectionRecord!=null){
                baseWrapper.setData(inspectionRecord);
                baseWrapper.setMsg("查询成功");
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
            }
        }
        return  baseWrapper;
    }

    /**
     * @Author 程叶同
     * @Description 查询全部
     * @Date 14:10 2018/1/10
     */
    public BaseWrapper getList(HttpServletRequest request, Integer page, Integer pageSize, String startTime, String endTime, String subCounty, String problemlev){
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        BaseWrapper baseWrapper=new BaseWrapper();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        map.put("startTime",startTime);
        map.put("endTIme",endTime);
        map.put("problemlev",problemlev);
        map.put("subCounty",subCounty);
        map1.put("subCounty",subCounty);
        map1.put("startTime",startTime);
        map1.put("endTIme",endTime);
        map1.put("problemlev",problemlev);

        List<InspectionRecord> inspectionRecords = inspectionRecordMapper.selectList(map);
        Integer total = inspectionRecordMapper.totalItems(map1);
        if(inspectionRecords!=null){
            for(InspectionRecord vr:inspectionRecords){
                List<Attachment> attachmentbefore = GetAttachmentListUtil.returnAttachment(vr.getAttachmentNameBefore(), vr.getAttachmentIdBefore(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                List<Attachment> attachmentafter = GetAttachmentListUtil.returnAttachment(vr.getAttachmentNameAfter(), vr.getAttachmentIdAfter(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                vr.setAttachmentListBefore(attachmentbefore);
                vr.setAttachmentListAfter(attachmentafter);
            }
            baseWrapper.setStatus(true);
            baseWrapper.setData(inspectionRecords);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("ok");
            baseWrapper.setTotle(total);
        }
        return  baseWrapper;
    }
}
